import Axios from 'axios'
import { Notification, Loading } from 'element-ui'

let service
const reqInstance = Axios.create({
    baseURL: '/api',
    timeout: 5000
})

reqInstance.interceptors.request.use((config) => {
    service = Loading.service()
    let token = window.sessionStorage.getItem('token')
    if (token) {
        config.headers['token'] = token
    }
    return config
})

reqInstance.interceptors.response.use(res => {
    service.close()
    console.log(res)
    let token = res?.data?.result?.token;
    if (token) {
        window.sessionStorage.setItem('token', token)
    }
    return res
}, err => {
    Notification.error(err.message)
    return Promise.reject(err)
})

export default reqInstance